﻿$(function () {
    //页面切换初始化
    $.fn.fullpage({
        slidesColor: ['#f7f7f9', '#f7f7f9', '#f7f7f9', '#f7f7f9', '#f7f7f9'],
        anchors: ['other', 'product', 'recentnew', 'contact'],
        menu: '#header',
        loopBottom: true,
        afterLoad: function (anchorLink, index) {
            pageSwitchFunc.afterLoad(anchorLink, index);
        },
        onLeave: function (index, direction, prevIndex) {
            pageSwitchFunc.onLeave(index, direction, prevIndex);
        }
    });

    //页面切换回调处理
    var pageSwitchFunc = (function () {
        var logo = $("#logo"), header = $("#header"),
            productList = $("#productList").find("div.productItem"),
            firstProduct = productList.eq(0),
            lastProduct = productList.eq(productList.length - 1);

        //导航菜单扩展
        var headerUpFunc = function () {
            logo.fadeOut(100, function () {
                header.animate({
                    "height": "104px"
                }, 300, null, function () {
                    logo.css({
                        "position": "relative",
                        "left": "0px",
                        "top": "0px"
                    }).fadeIn(100);
                });
            });
        };
        //导航菜单收缩
        var headerDownFunc = function () {
            logo.fadeOut(100, function () {
                header.animate({
                    "height": "64px"
                }, 300, null, function () {
                    logo.css("position", "absolute").animate({
                        "left": "20px",
                        "bottom": "0px"
                    }, 10).fadeIn(100);
                });
            });
        };
        //导航菜单激活
        var headerNavActive = function (anchorLink) {
            $("#navMenu").find("a[href$=" + anchorLink + "]").addClass("current")
                .siblings().removeClass("current");
        };
        //产品图片伸展
        var productImgExpand = function (anchorlink, index) {
            var pos = "3%", speed = 400;
            firstProduct.animate({
                "left": pos
            }, speed);
            lastProduct.animate({
                "right": pos
            }, speed);
        };
        //产品图片收缩
        var productImgCospan = function () {
            var pos = "50%", speed = 400;
            firstProduct.animate({
                "left": pos
            }, speed);
            lastProduct.animate({
                "right": pos
            }, speed);
        };

        return {
            afterLoad: function (anchorlink, index) {
                //导航菜单激活
                headerNavActive(anchorlink);
                //进入产品区域图片伸展
                if (anchorlink == "product") {
                    productImgExpand();
                }
            },
            onLeave: function (index, direction, prevIndex) {
                if (direction == "down") {
                    if (index == 1) {
                        headerDownFunc();
                    }
                }
                if (direction == "up") {
                    if (index == 0) {
                        headerUpFunc();
                    }
                }
                if (prevIndex == 1) {
                    productImgCospan();
                }
            }
        };
    })();

    (function () {
        //定位到下一屏
        $(".nextsection").on("click", function () {
            $.fn.fullpage.moveSlideDown();
        });

        //点击Logo回到首屏
        $("#logo>a").on("click", function () {
            $.fn.fullpage.moveToSlide('other');
        });

        //产品详细Slide横向切换
        $("#sectionProduct").find("a.product_item").on("click", function () {
            var slideAnchor = $(this).attr("href");
            if (slideAnchor) {
                var anchors = slideAnchor.split("/");
                if (anchors.length == 2) {
                    var slide = anchors[0];
                    var index = anchors[1];
                    console.log("slide:" + slide + ",index:" + index);
                    $.fn.fullpage.moveSlideLandscape(slide, index);
                }
            }
            return false;
        });
    })();
});